package me.panpf.sketch;

import me.panpf.sketch.decode.ResizeCalculator;
import me.panpf.sketch.util.Utils;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.components.Image;
import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.render.PixelMapHolder;
import ohos.agp.utils.RectFloat;
import ohos.app.Context;
import ohos.media.image.ImageSource;
import ohos.media.image.PixelMap;

public class ShapeSizeImage extends Image implements Component.DrawTask {
    private Paint paint;
    private RectFloat srcRect;
    private ResizeCalculator resizeCalculator;
    private RectFloat bounds;
    private ImageView.ScaleType scaleType = ImageView.ScaleType.ZOOM_CENTER;
    private String rawPath = null;

    public ShapeSizeImage(Context context) {
        this(context, null);
    }

    public ShapeSizeImage(Context context, AttrSet attrSet) {
        this(context, attrSet, "");
    }

    public ShapeSizeImage(Context context, AttrSet attrSet, String styleName) {
        super(context, attrSet, styleName);
        addDrawTask(this);
        setPixelMap(Utils.createTransparentPixelMap(1920, 1080));
        paint = new Paint();
        paint.setFilterBitmap(true);
        paint.setDither(true);
        srcRect = new RectFloat();
        bounds = new RectFloat(0, 0, 500, 500);
        this.resizeCalculator = new ResizeCalculator();
    }

    private PixelMap getPixelMap(String path) {
        ImageSource imageSource = Utils.getImageSource(getContext(), path);
        return imageSource.createPixelmap(null);
    }

    @Override
    public void addDrawTask(DrawTask task) {
        super.addDrawTask(task);
        task.onDraw(this, mCanvasForTaskOverContent);
    }

    @Override
    public void onDraw(Component component, Canvas canvas) {
        PixelMap pixelMap = rawPath == null ? null : getPixelMap(rawPath);
        canvas.drawPixelMapHolderRect(new PixelMapHolder(pixelMap), srcRect != null && !srcRect.isEmpty() ? srcRect : null, bounds, paint);
    }

    /**
     * 边界变化
     *
     * @param width  宽度
     * @param height 高度
     */
    public void onBoundsChange(int width, int height) {
        int boundsWidth = width;
        int boundsHeight = height;
        bounds.right = width;
        bounds.bottom = height;
        int bitmapWidth = 1920;
        int bitmapHeight = 1080;

        if (boundsWidth == 0 || boundsHeight == 0 || bitmapWidth == 0 || bitmapHeight == 0) {
            srcRect.clear();
        } else if (Math.abs((double) bitmapWidth / (double) bitmapHeight - (double) boundsWidth / (double) boundsHeight) <= 0) {
            srcRect.left = 0;
            srcRect.top = 0;
            srcRect.right = bitmapWidth;
            srcRect.bottom = bitmapHeight;
        } else {
            ResizeCalculator.Mapping mapping = resizeCalculator.calculator(bitmapWidth, bitmapHeight, boundsWidth, boundsHeight, scaleType, true);
            srcRect.left = mapping.srcRect.left;
            srcRect.top = mapping.srcRect.top;
            srcRect.right = mapping.srcRect.right;
            srcRect.bottom = mapping.srcRect.bottom;
        }

        Utils.error("srcRect = " + srcRect.toString() + ", bounds = " + bounds.toString());

        invalidate();

    }

    /**
     * 设置原始路径
     *
     * @param rawPath 原始路径
     */
    public void setRawPath(String rawPath) {
        this.rawPath = rawPath;
    }

    /**
     * 设置缩放类型
     *
     * @param scaleType 缩放类型
     */
    public void setScaleType(ImageView.ScaleType scaleType) {
        this.scaleType = scaleType;
    }
}
